import MyPromise from 'components/Promise';

// console.log(111);
// const aa = new Promise((resolve) => {
//   console.log(222);
//   setTimeout(() => {
//     console.log('timeout');
//     resolve()
//   }, 2000);
// }).then(res => console.log(333))
// console.log(444);

/**
 * 1. Promise 基础实现
 */
let p = new MyPromise((resolve, reject) => {
  console.log('\n------ 1. Promise 基础实现');
  resolve('ok');
  reject('no');
});

p.then(
  (res) => {
    console.log(1, 'success', res);
  },
  (err) => {
    console.log(1, 'fail', err);
  }
); // 1, success, ok

/**
 * 2. 处理异步情况
 */
p = new MyPromise((resolve, reject) => {
  console.log('\n------ 2. 处理异步情况');
  setTimeout(() => {
    resolve('ok');
  }, 2000);
  // reject('no') // 2, fail, no
});

p.then(
  (res) => {
    console.log(2, 'success', res);
  },
  (err) => {
    console.log(2, 'fail', err);
  }
); // 2, success, ok

/**
 * 3. then 函数多次调用添加多个处理函数
 */
p.then(
  (res) => {
    console.log('\n------ 3. then 函数多次调用添加多个处理函数');
    console.log(3.1, 'success', res);
  },
  (err) => {
    console.log(3.1, 'fail', err);
  }
);
p.then(
  (res) => {
    console.log(3.2, 'success', res);
  },
  (err) => {
    console.log(3.2, 'fail', err);
  }
);
p.then(
  (res) => {
    console.log(3.3, 'success', res);
  },
  (err) => {
    console.log(3.3, 'fail', err);
  }
);

/**
 * 4. then 函数的链式调用（一）
 */
p.then(
  (res) => {
    console.log('\n------ 4. then 函数的链式调用（一）');
    console.log(4.1, 'success', res);
    return 4.1;
  },
  (err) => {
    console.log(4.1, 'fail', err);
    return 4.1;
  }
)
  .then(
    (res) => {
      console.log(4.2, 'success', res);
      return 4.2;
    },
    (err) => {
      console.log(4.2, 'fail', err);
      return 4.2;
    }
  )
  .then(
    (res) => {
      console.log(4.3, 'success', res);
    },
    (err) => {
      console.log(4.3, 'fail', err);
    }
  );

/**
 * 5. then 函数的链式调用（二）
 */
const p1 = new MyPromise((resolve, reject) => {
  resolve('p1');
});
const other = () => {
  return new MyPromise((resolve, reject) => {
    resolve('other');
  });
};
p.then(
  (res) => {
    console.log('\n------ 5. then 函数的链式调用（二）');
    console.log(5.1, 'success', res);
    return other();
  },
  (err) => {
    console.log(5.1, 'fail', err);
  }
).then(
  (res) => {
    console.log(5.2, 'success', res);
  },
  (err) => {
    console.log(5.2, 'fail', err);
  }
);

/**
 * 6. then 方法链式调用识别 Promise 对象自返回
 */
const p2 = p1.then(
  (res) => {
    console.log('\n------ 6. then 方法链式调用识别 Promise 对象自返回');
    console.log(6.1, 'success', res);
    return p2;
  },
  (err) => {
    console.log(6.1, 'fail', err);
  }
);
p2.then(
  (res) => {
    console.log(6.2, 'success', res);
  },
  (err) => {
    console.log(6.2, 'fail', err);
  }
);

/**
 * 7. 捕获错误及 then 链式调用
 */
p.then(
  (res) => {
    console.log('\n------ 7. 捕获错误及 then 链式调用');
    console.log(7.1, 'success', res);
    return new MyPromise((resolve, reject) => {
      throw new Error('p3 exector error');
    });
  },
  (err) => {
    console.log(7.1, 'fail', err);
  }
).then(
  (res) => {
    console.log(7.2, 'success', res);
  },
  (err) => {
    console.log(7.2, 'fail', err);
  }
);

/**
 * 8. 将 then 方法的参数变成可选参数
 */
p.then(
  (res) => {
    console.log('\n------ 8. 将 then 方法的参数变成可选参数');
    console.log(8.1, 'success', res);
    return 8.1;
  },
  (err) => {
    console.log(8.1, 'fail', err);
  }
)
  .then()
  .then()
  .then(
    (res) => {
      console.log(8.2, 'success', res);
    },
    (err) => {
      console.log(8.2, 'fail', err);
    }
  );

/**
 * 9. Promise.all 方法的实现
 */
const pp1 = new MyPromise((resolve) => {
  console.log('\n------ 9. Promise.all 方法的实现');
  setTimeout(() => {
    resolve('pp1');
  }, 1000);
});
const pp2 = new MyPromise((resolve) => {
  resolve('pp2');
});
MyPromise.all([9, pp1, pp2]).then(
  (res) => {
    console.log(9, 'success', res);
  },
  (err) => {
    console.log(9, 'fail', err);
  }
);

/**
 * 10. Promise.resolve 方法的实现
 */

MyPromise.resolve(10).then((res) => {
  console.log('\n------ 10. Promise.resolve 方法的实现');
  console.log(res);
});
MyPromise.reject(10.1).then(
  (res) => {
    console.log(10.1, 'success', res);
  },
  (err) => {
    console.log(10.1, 'fail', err);
  }
);

/**
 * 11. finally 方法的实现
 */
p.finally(123).then((res) => {
  console.log('\n------ 11. finally 方法的实现');
  console.log(11, 'success', res);
});
MyPromise.reject(11.1).then(undefined, (error) => {
  console.log(11.1, 'fail', error);
});

/**
 * 12. catch 方法的实现
 */
MyPromise.reject(12).catch((err) => {
  console.log('\n------ 12. catch 方法的实现');
  console.log(12, 'catch', err);
});
